import com.lzy.pojo.bus;
import com.lzy.pojo.flight;
import com.lzy.pojo.hotels;
import com.lzy.pojo.reservation;
import com.lzy.service.busService;
import com.lzy.service.flightService;
import com.lzy.service.hotelsService;
import com.lzy.service.impl.busServiceImpl;
import com.lzy.service.impl.flightServiceImpl;
import com.lzy.service.impl.hotelsServiceImpl;
import com.lzy.service.impl.reservationServiceImpl;
import com.lzy.service.reservationService;

import java.sql.SQLOutput;
import java.util.List;
import java.util.Scanner;

public class finalTest {
    private static flightService flightService=new flightServiceImpl();
    private static hotelsService hotelsService=new hotelsServiceImpl();
    private static busService busService=new busServiceImpl();
    private static reservationService reservationService=new reservationServiceImpl();
    public static void showFlights(){
        System.out.println("Flight details are as follows:");
        List<flight> flights = flightService.selectAll();
        for (flight flight : flights) System.out.println(flight);
    }
    public static void showHotels(){
        System.out.println("Hotels details are as follows:");
        List<hotels> hotels = hotelsService.selectAll();
        for (hotels hotel : hotels) System.out.println(hotel);
    }
    public static void showBuses(){
        System.out.println("Buses details are as follows:");
        List<bus> buses = busService.selectAll();
        for (bus bus : buses) System.out.println(bus);
    }
    public static void choice(){
        System.out.println("Select an action:");
        System.out.println("1.Book flights, buses, hotel rooms");
        System.out.println("2.Check flights, buses, hotel rooms, customers and bookings");
        System.out.println("3.Check a customer's travel line");
        System.out.println("4.Check the integrity of line");
        System.out.println("5.Delete the booking information");
        System.out.println("6.Exit");
        System.out.print("Your choice is:");
    }
    public static void booking(){
        System.out.println("Please enter the type of reservation required");
        System.out.println("1 is flight,2 is hotel,3 is bus");
        System.out.print("The type you need is:");
        Scanner scanner=new Scanner(System.in);
        int type=scanner.nextInt();
        if (type==1){
            showFlights();
            System.out.print("Please enter your name:");
            String name=scanner.next();
            System.out.print("Please enter your flight number");
            String number=scanner.next();
            flight flight = flightService.select(number);
            if (flight==null){
                System.out.println("There is no such flight");
                return;
            }
            reservation reservation=new reservation(name,1,number);
            reservationService.insert(reservation);
            System.out.println("Booking successful");
        }
        if (type==2){
            showHotels();
            System.out.print("Please enter your name:");
            String name=scanner.next();
            System.out.print("Please enter your hotel number:");
            String number=scanner.next();
            hotels hotels1 = hotelsService.select(number);
            if (hotels1==null){
                System.out.println("There is no such hotel");
                return;
            }
            reservation reservation=new reservation(name,2,number);
            reservationService.insert(reservation);
            System.out.println("booking success");
        }
        if (type==3){
            showBuses();
            System.out.print("Please enter your name:");
            String name=scanner.next();
            System.out.print("Please enter your bus number");
            String number=scanner.next();
            bus bus = busService.select(number);
            if (bus==null){
                System.out.println("There is no such bus");
                return;
            }
            reservation reservation=new reservation(name,3,number);
            reservationService.insert(reservation);
        }
    }
    public static void checkIfFull(){
        System.out.println("Please enter query options");
        System.out.println("1 is flight,2 is hotel,3 is bus");
        System.out.print("The type you need is:");
        Scanner scanner=new Scanner(System.in);
        int type=scanner.nextInt();
        if (type==1) showFlights();
        if (type==2) showHotels();
        if (type==3) showBuses();
    }
    public static void checkLine(){
        System.out.println("Please enter the name of the customer");
        System.out.print("The customer's name is:");
        Scanner scanner=new Scanner(System.in);
        String name=scanner.next();
        List<String> keys = reservationService.selectKeys(name);
        List<String> types = reservationService.selectTypes(name);
        for (int i = 0; i < keys.size(); i++) {
            if (types.get(i).equals("1")){
                flight flight = flightService.select(keys.get(i));
                System.out.println("from city is:"+flight.getFromCity()+",arrive city is:"+flight.getArivCity());
            }
            if (types.get(i).equals("2")){
                hotels hotels = hotelsService.select(keys.get(i));
                System.out.println("The hotel is located in:"+hotels.getLocation());
            }
            if (types.get(i).equals("3")){
                bus bus=busService.select(keys.get(i));
                System.out.println("from city is:"+bus.getLocation()+",arrive city is:"+bus.getDestination());
            }
        }
    }
    public static void checkIntegrityOfLine(){
        System.out.println("Please enter query options");
        System.out.println("1 is flight,2 is hotel,3 is bus");
        System.out.print("The type you need is:");
        Scanner scanner=new Scanner(System.in);
        int type=scanner.nextInt();
        if (type==1){
            System.out.print("Please enter flight number:");
            String number=scanner.next();
            flight flight = flightService.select(number);
            while (flight==null){
                System.out.print("The flight does not exist,please re-enter it:");
                String newNumber=scanner.next();
                flight = flightService.select(newNumber);
            }
            if (flight.getNumAvail()>=0){
                System.out.println("Plenty of seats on the flight");
                return;
            }
        }
        if (type==2){
            System.out.print("Please enter hotel number:");
            String number1=scanner.next();
            hotels hotels = hotelsService.select(number1);
            while (hotels==null){
                System.out.print("The hotel does not exist,please re-enter it:");
                String newNumber=scanner.next();
                hotels = hotelsService.select(newNumber);
            }
            if (hotels.getNumAvail()>=0){
                System.out.println("Plenty of rooms in the hotel");
                return;
            }
        }
        if (type==3){
            System.out.print("Please enter bus number:");
            String number2=scanner.next();
            bus bus = busService.select(number2);
            while (bus==null){
                System.out.print("The bus does not exist,please re-enter it:");
                String newNumber=scanner.next();
                bus = busService.select(newNumber);
            }
            if (bus.getNumAvail()>=0){
                System.out.println("Plenty of seats in the bus");
                return;
            }
        }
    }
    public static void deleteBooking(){
        System.out.print("Please enter your name:");
        Scanner scanner=new Scanner(System.in);
        String name=scanner.next();
        System.out.print("Please enter the specific number:");
        String number=scanner.next();
        reservation reservation = reservationService.selectByNameAndNumber(name, number);
        while (reservation==null){
            System.out.println("Customer does not exit,,please re-enter");
            System.out.print("Please enter your name:");
            name=scanner.next();
            System.out.print("Please enter the specific number:");
            number=scanner.next();
            reservation=reservationService.selectByNameAndNumber(name,number);
        }
        reservationService.delete(name,number);
        System.out.println("Delete success");
    }
    public static void main(String[] args) {
        choice();
        Scanner input = new Scanner(System.in);
        int choice;
        choice=input.nextInt();
        while (choice!=6){
            if (choice==1) booking();
            if (choice==2) checkIfFull();
            if (choice==3) checkLine();
            if (choice==4) checkIntegrityOfLine();
            if (choice==5) deleteBooking();
            if (choice==6){
                    System.out.println("Thanks for using the system");
                    return;
                }
            choice();
            choice=input.nextInt();
        }
    }
}
